package com.tledu.springboot04.controlller;

import com.tledu.springboot04.constants.CommonConstans;
import com.tledu.springboot04.core.result.AjaxResult;
import com.tledu.springboot04.domain.User;
import com.tledu.springboot04.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author Antg
 * @date 2021/8/26  14:58
 */
@Controller
@AllArgsConstructor
public class LoginController {
    private final UserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(String username, String password, HttpSession session) {
        User loginUser = userService.login(username, password);
        session.setAttribute(CommonConstans.LOGIN_USER, loginUser);
        return new AjaxResult("登录成功");
    }
}
